package com.lanou.wuyou.gift.home;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lanou.wuyou.gift.R;
import com.lanou.wuyou.gift.bean.HomeFirstTitleViewBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by dllo on 17/3/22.
 */

public class HomeFirstTopViewAdapter extends PagerAdapter{

    private Context context;
    private HomeFirstTitleViewBean bean;

    public void setBean(HomeFirstTitleViewBean data) {
        this.bean = data;
        notifyDataSetChanged();
    }

    public HomeFirstTopViewAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        if(bean == null){
            return 0;
        }
        if (bean.getData() == null) {
            return 0;
        }
        return  bean.getData().getBanners().size() > 0 ? bean.getData().getBanners().size() + 2 : 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.viewpager_view,container,false);
        ImageView imageView = (ImageView) view.findViewById(R.id.view_viewpager);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<HomeFirstTitleViewBean.DataBean.BannersBean> targetBean = bean.getData().getBanners();
                        if (targetBean != null){
                            int id = targetBean.get(position).getTarget().getId();
                            Intent intent = new Intent(context,HomeHeadViewPagerWebView.class);
                            intent.putExtra("id",id);
                            context.startActivity(intent);
                        }
            }
        });
        imageView.setAdjustViewBounds(true);
        container.addView(view);

        List<HomeFirstTitleViewBean.DataBean.BannersBean> banners = bean.getData().getBanners();

        HomeFirstTitleViewBean.DataBean.BannersBean bean = null;
        //position为0（实际图片）显示最后一张图（视觉上的）
        if (position == 0) {
            bean = banners.get(banners.size() - 1);
            //position为总数减去1显示的是
        } else if (position == getCount() - 1) {
            bean = banners.get(0);
            //其他情况
        } else {
            bean = banners.get(position - 1);
        }
        //网络请求图片
        Picasso.with(context).load(bean.getImage_url()).into(imageView);
        return view;

    }



    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }
}
